// const { ELoadAction } = require("XrFrame");

// pages/judgeitem/judgeitem.js
Page({


  data: {
    itemid:"",
    orderid:"",
    pic:"",
    //评论等级
    judgelevel:[
      {
        level:'差',
        thislevel:false
      },{
        level:'一般',
        thislevel:false
      },{
        level:'好',
        thislevel:false
      }
    ],
    level:'',
    judgetext:'',
    picurls:[],
    showpic:"",
    isjudge:""
  },
  onLoad(options){

    var that = this;
    //记录当前订单id 用来改变状态
    this.setData({
      itemid:options.itemid,
      orderid:options.orderid
    })
    if(options.orderid && options.itemid){
      wx.request({
        url: 'http://127.0.0.1:8080/itemJudge/selectOrderJudge',
        method:"POST",
        data:{orderid:options.orderid},
        success(res){
          if(res.data.length != 0){
            var data = res.data[0];
            // console.log(data)
            var pic = JSON.parse(data.itemJudgePic)

            for(var i=0;i<that.data.judgelevel.length;i++){
              var change = 'judgelevel['+i+'].thislevel';
              if(data.itemJudgeRank == that.data.judgelevel[i].level){
                  that.setData({
                  [change]:true
                  })
              }
            }
           
            that.setData({
              isjudge:true,
              judgetext:data.itemJudgeText,
              picurls:pic,
              showpic:true
            })
          }
          // console.log(res)
        }
      })
        //物品图片渲染
        wx.request({
          url: 'http://127.0.0.1:8080/item/selectItemById',
          method:"POST",
          data:{itemid:options.itemid},
          success(res){
            // console.log(res)
            if(res.data){
              var data = res.data[0];
              data.itemPic = JSON.parse(data.itemPic)
              that.setData({
                // 物品信息
                pic:data.itemPic[0],
              })
            }
          }
        })
    }
  },

  // 选择评价等级
  chooselevel(e){
    var index = e.currentTarget.dataset.index;
    var level = 'judgelevel['+index+'].thislevel';
    this.setData({
      [level]:true,
      level:this.data.judgelevel[index].level
    })
    for(var i=0;i<this.data.judgelevel.length;i++){
        if(index != i){
          var level = 'judgelevel['+i+'].thislevel';
          this.setData({
            [level]:false
          })
        }
    }
  },

  // 获取商品评价
  getjudgetext(e){
    this.setData({
      judgetext:e.detail.value
    })
  },

  // 添加商品快照
  addpic(e){
    let that = this;
    wx.chooseImage({
      count: 9,
      success(res){
        var picarray = that.data.picurls.concat(res.tempFilePaths)
        console.log(picarray)
        that.setData({
          picurls:picarray,
          showpic:true
        })
      }
    })
  },
  //删除照片
  delpic(e){
    console.log(e)
    let that = this;
    var index = e.currentTarget.dataset.index;
    var del = this.data.picurls
    wx.showModal({
      content: '是否删除该照片',
      title: '提示',
      success(res){
        if(res.confirm){
          del.splice(index,1)
          var len = del.length
          if(len == 0){
            that.setData({
              showpic:false
            })
          }
          that.setData({
            picurls:del
          })
        }
      },
    })
    
  },
  // 查看图片
  showpic(e){
    var index  =e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.picurls[index],     //当前图片地址
      urls: this.data.picurls,                 //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  submit(){
    wx.showModal({
      title: '确定发布评论吗?',
      complete: (res) => {
        if (res.confirm) {
          var data = this.data;
            //上传完成后把文件上传到服务器
            var arr = []
            var h = this.data.picurls.length;
            var i =0;
            for (i = 0; i < h; i++) {
            //上传文件
            // console.log(i,h)
              wx.uploadFile({
              url: 'http://127.0.0.1:8080/image/upload',
              filePath: this.data.picurls[i],
                name: "file",
                header: {
                  "content-type": "multipart/form-data"
                },
                success: function ({ data: res }) {
                  // console.log(res)
                  res = JSON.parse(res);
                  const pic_url = 'http://127.0.0.1:8080/image/download?name=' + res.data
                  // console.log(pic_url)
                  // 图片路径集合
                  arr.unshift(pic_url);
                },
                fail: function (err) {
                  wx.showToast({
                    title: "上传失败",
                    icon: "error",
                    duration: 2000
                  })
                },
                complete: function (result) {
                  // console.log(result.errMsg)
                }
              })
            }

            setTimeout(function(){
              var changeItemTime = new Date().toJSON().substring(0, 10) + ' ' + new Date().toTimeString().substring(0,8);

              var data1={
                level:data.level,
                judgetext:data.judgetext,
                judgepic:JSON.stringify(arr),
                judgetime:changeItemTime,
                userid:wx.getStorageSync('openid'),
                itemid:data.itemid,
                orderid:data.orderid
              }
              // console.log(data1)
              wx.request({
                url: 'http://127.0.0.1:8080/itemJudge/submititemjudge',
                method:"POST",
                data:data1,
                success(res){
                  // console.log(res)
                  if(res.data){
                    wx.request({
                      url: 'http://127.0.0.1:8080/order/orderForJudge',
                      method:"POST",
                      data:{orderid:data.orderid},
                      success(res){
                        console.log(res);
                        if(res.data){
                          wx.navigateBack({
                            delta: 2,
                            success: (res) => {
                              wx.showToast({
                                title: '评价成功',
                                icon:"success"
                              })
                            },
                          })
                        }
                      }
                    })
                    
                  }
                }
              })

            },100)
          
        }
      }
    })
  }

})